<script setup lang="ts">
import {reactive,ref} from "vue"
import {useUserStore} from '@/store/modules/user'
import {ElMessage, ElNotification} from "element-plus";
import {useRouter} from "vue-router";

let loginParam = reactive({username: '', password: ''})
const $router = useRouter()
let loading = ref(false)

const userStore = useUserStore();
const loginByUsername = async () => {
  if (loading.value) return
  loading.value = true
  try {
    await userStore.login(loginParam)
    ElMessage({
      message: '登录成功',
      type: 'success',
    })
    await $router.push('/home')
  } catch (e) {
    ElNotification({
      message: e as string,
      type: 'error'
    })
  } finally {
    loading.value = false
  }
}
</script>

<template>
  <div class="login_container">
    <div class="login">
      <el-input
          class="loginParam"
          v-model="loginParam.username"
          placeholder="请输入用户名"/>
      <el-input
          class="loginParam"
          v-model="loginParam.password"
          type="password"
          placeholder="请输入密码"
          show-password/>
      <br>
      <div class="mb-4" @click="loginByUsername">
        <el-button type="primary" :loading="loading" plain style="width: 180px">登录</el-button>
      </div>
    </div>
  </div>
</template>

<style scoped>
.login_container {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100vh; /* 占满整个视口高度 */
  background-image: url("@/assets/images/login.png");
  background-size: cover; /* 自动缩放，覆盖全屏 */
  background-position: center;
}

.login {
  margin-top: 300px;
  margin-left: 40%;
  width: 240px;
  height: 150px;
  border: 1px solid black;
}

.loginParam {
  margin-top: 10px;
  margin-left: 10px;
  width: 180px;
}

.mb-4 {
  margin-top: 10px;
  margin-left: 10px;
}

</style>